//
//  NCButton.swift
//  ZZUI
//
//  Created by 张文晏 on 2018/11/12.
//  Copyright © 2018 张文晏. All rights reserved.
//

import UIKit

public class NCButton: UIButton {
    public enum Style {
        case theme
        case gray
        case warning
        case border
        case module
    }
    
    public override var isEnabled: Bool { didSet { setupUI() } }
    /// 风格
    public var style = Style.theme { didSet { setupUI() } }
    
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    
    func setup() {
        self.addTarget(self, action: #selector(touchDown), for: .touchDown)
        self.addTarget(self, action: #selector(touchUp), for: .touchUpInside)
        self.addTarget(self, action: #selector(touchUp), for: .touchUpOutside)
        setupUI()
    }
    
    func setupUI() {
        layer.cornerRadius = 3
        switch style {
        case .theme:
            layer.borderWidth = 0
            layer.borderColor = nil
            backgroundColor = isEnabled ? UIColor(hex: 0x006fff) : UIColor(hex: 0x006fff, alpha: 0.6)
            setTitleColor(.white, for: .normal)
            setTitleColor(UIColor(hex: 0xffffff, alpha: 0.25), for: .highlighted)
            setTitleColor(UIColor(hex: 0xffffff, alpha: 0.65), for: .disabled)
        case .gray:
            layer.borderWidth = 0
            layer.borderColor = nil
            backgroundColor = isEnabled ? UIColor(hex: 0xf5f5f5) : UIColor(hex: 0xf5f5f5, alpha: 0.6)
            setTitleColor(UIColor(hex: 0x666666), for: .normal)
            setTitleColor(UIColor(hex: 0x666666, alpha: 0.25), for: .highlighted)
            setTitleColor(UIColor(hex: 0x666666, alpha: 0.6), for: .disabled)
        case .warning:
            layer.borderWidth = 0
            layer.borderColor = nil
            backgroundColor = isEnabled ? UIColor(hex: 0xf5222d) : UIColor(hex: 0xf5222d, alpha: 0.6)
            setTitleColor(.white, for: .normal)
            setTitleColor(UIColor(hex: 0xffffff, alpha: 0.25), for: .highlighted)
            setTitleColor(UIColor(hex: 0xffffff, alpha: 0.65), for: .disabled)
        case .border:
            layer.borderWidth = 1
            layer.borderColor = isEnabled ? UIColor(hex: 0x006fff).cgColor : UIColor(hex: 0xdddddd).cgColor
            backgroundColor = .white
            setTitleColor(UIColor(hex: 0x006fff), for: .normal)
            setTitleColor(UIColor(hex: 0x1c5aac), for: .highlighted)
            setTitleColor(UIColor(hex: 0xdddddd), for: .disabled)
        case .module:
            layer.borderWidth = 0
            layer.borderColor = nil
            backgroundColor = isEnabled ? UIColor(hex: 0xe5f0ff) : UIColor(hex: 0xe5f0ff, alpha: 0.65)
            setTitleColor(UIColor(hex: 0x006fff), for: .normal)
            setTitleColor(UIColor(hex: 0x006fff, alpha: 0.25), for: .highlighted)
            setTitleColor(UIColor(hex: 0x006fff, alpha: 0.25), for: .disabled)
        }
    }
    
    @objc func touchDown() {
        guard isEnabled else { return }
        switch style {
        case .theme:
            backgroundColor = UIColor(hex: 0x1c5aac)
        case .gray:
            backgroundColor = UIColor(hex: 0xdddddd)
        case .warning:
            backgroundColor = UIColor(hex: 0xbb161e)
        case .border:
            layer.borderColor = UIColor(hex: 0x1c5aac).cgColor
        case .module:
            backgroundColor = UIColor(hex: 0x1c5aac, alpha: 0.1)
        }
    }
    
    @objc func touchUp() {
        guard isEnabled else { return }
        switch style {
        case .theme:
            backgroundColor = UIColor(hex: 0x006fff)
        case .gray:
            backgroundColor = UIColor(hex: 0xf5f5f5)
        case .warning:
            backgroundColor = UIColor(hex: 0xf5222d)
        case .border:
            layer.borderColor = UIColor(hex: 0x006fff).cgColor
        case .module:
            backgroundColor = UIColor(hex: 0xe5f0ff)
        }
    }
    
    public func set(font: UIFont) {
        titleLabel?.font = font
    }
}
